package frame.managermeau.add;

import frame.model.SnowflakeIdWorker;

import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class AddInsuranceFrame extends JFrame {
    Connection con;
    SnowflakeIdWorker idWorker;
    public void init() {
        try {
            Class.forName("org.opengauss.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            con = DriverManager.getConnection("jdbc:opengauss://121.36.41.207:26000/finance", "geqiyu", "Tlyz0729");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            // 设置db1为搜索路径
            PreparedStatement firstStep = con.prepareStatement("SET SEARCH_PATH TO finance,public");
            firstStep.execute();
            firstStep.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        idWorker = new SnowflakeIdWorker(0, 0);
    }
    public AddInsuranceFrame() {
        this.setTitle("保险");
        //操作
        this.setContentPane(showNav());
        //设置窗口
        this.setBounds(580,280,400,400);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    /**
     * 显示UI，不用改变
     * @return
     */
    public JPanel showNav() {
        JPanel jPanel = new JPanel(null);
        int x = 30,xText = 130, y = 20;
        int width = 100, height = 20;

        JLabel jl;
        //名称
        jl = new JLabel("名称:");
        jl.setLocation(x,y);
        jl.setSize(width,height);
        jPanel.add(jl);
        JTextArea jTextAreaName = new JTextArea(1,20);
        jTextAreaName.setLocation(xText,y);
        jTextAreaName.setSize(200,20);
        jPanel.add(jTextAreaName);
        y += 40;

        //最低年龄
        jl = new JLabel("最低年龄:");
        jl.setLocation(x,y);
        jl.setSize(width,height);
        jPanel.add(jl);
        JTextArea jTextAreaMinAge = new JTextArea(1,20);
        jTextAreaMinAge.setLocation(xText,y);
        jTextAreaMinAge.setSize(200,20);
        jPanel.add(jTextAreaMinAge);
        y += 40;

        //最高年龄
        jl = new JLabel("最高年龄:");
        jl.setLocation(x,y);
        jl.setSize(width,height);
        jPanel.add(jl);
        JTextArea jTextAreaMaxAge = new JTextArea(1,20);
        jTextAreaMaxAge.setLocation(xText,y);
        jTextAreaMaxAge.setSize(200,20);
        jPanel.add(jTextAreaMaxAge);
        y += 40;

        //金额
        jl = new JLabel("金额:");
        jl.setLocation(x,y);
        jl.setSize(width,height);
        jPanel.add(jl);
        JTextArea jTextAreaMoney = new JTextArea(1,20);
        jTextAreaMoney.setLocation(xText,y);
        jTextAreaMoney.setSize(200,20);
        jPanel.add(jTextAreaMoney);
        y += 40;

        //保险项目
        jl = new JLabel("保险项目：");
        jl.setLocation(x,y);
        jl.setSize(width,height);
        jPanel.add(jl);
        JTextArea jTextAreaProject = new JTextArea(1,20);
        jTextAreaProject.setLocation(xText,y);
        jTextAreaProject.setSize(200,20);
        jPanel.add(jTextAreaProject);
        y += 40;

        //保险时间
        jl = new JLabel("保险时间：");
        jl.setLocation(x,y);
        jl.setSize(width,height);
        jPanel.add(jl);
        JTextArea jTextAreaAgainPeriod = new JTextArea(1,20);
        jTextAreaAgainPeriod.setLocation(xText,y);
        jTextAreaAgainPeriod.setSize(200,20);
        jPanel.add(jTextAreaAgainPeriod);
        y += 40;

        //添加按钮与事件
        JButton jb = new JButton("增加");
        jb.setLocation(xText+30,y);
        jb.setSize(60,40);
        jb.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String name = jTextAreaName.getText();
                String minAge = jTextAreaMinAge.getText();
                String maxAge = jTextAreaMaxAge.getText();
                String price = jTextAreaMoney.getText();
                String project = jTextAreaProject.getText();
                String period = jTextAreaAgainPeriod.getText();

                if(name.length() == 0 || minAge.length() == 0 || maxAge.length() == 0 || price.length() == 0|| project.length() == 0 || period.length() == 0) {
                    JOptionPane.showMessageDialog(null, "请输入！");
                }else {
                    add(name, minAge, maxAge, price, project, period);
                }
            }
        });
        jPanel.add(jb);
        return jPanel;
    }


    /**
     * 添加保险的条目
     * @param name      名称
     * @param minAge    最低年龄
     * @param maxAge    最高年龄
     * @param price     金额
     * @param project   保险项目
     * @param period    保险时间
     */
    private void add(String name, String minAge, String maxAge, String price, String project, String period) {
        /*
        -------------------------------------------------------------------------------------------------------------------------------
            业务逻辑
        -------------------------------------------------------------------------------------------------------------------------------
         */

        //返回操作结果情况
        init();
        try {
            PreparedStatement preparedStatement=con.prepareStatement("INSERT INTO t_insurance(in_id,in_name,in_people_lower,in_people_upper,in_price,in_coverage,in_period,in_is_on_sale) values (?,?,?,?,?,?,?,?)");
            preparedStatement.setString(1,idWorker.nextId()+"22");
            preparedStatement.setString(2,name);
            preparedStatement.setInt(3,Integer.parseInt(minAge));
            preparedStatement.setInt(4,Integer.parseInt(maxAge));
            preparedStatement.setFloat(5,Float.parseFloat(price));
            preparedStatement.setString(6,project);
            preparedStatement.setInt(7,Integer.parseInt(period));
            preparedStatement.setBoolean(8,false);
            preparedStatement.execute();
        } catch (SQLException e) {
            e.printStackTrace();
            String message = "数据出错";
            JOptionPane.showMessageDialog(null, message);
        }
        String message = "注册成功";
        JOptionPane.showMessageDialog(null, message);
    }


    public static void main(String[] args) {
        new AddInsuranceFrame();
    }
}
